
var Login = new Class({
    Implements: [Events, Options],
    options: {
        tagid: null
    },
    initialize: function(options) {
        this.setOptions(options);
        this.loginPanel = document.id(this.options.tagid);
        if(!this.loginPanel) return;

        this.loginPanel.slide('hide');
        document.id('logtoggle').addEvents({
            'click': function(ev) {
                ev.stop();
                this.loginPanel.slide('toggle');
            }.bind(this),
            'focus': function() {this.blur();}
        });

        document.id('closePanel').addEvents({
            'click': this.hidePanel.bind(this),
            'focus': function() {this.blur();}
        });

        document.id('login').addEvents({
            'click': this.getUser.bind(this),
            'focus': function() {this.blur();}
        });
    },
    getUser: function(ev) {
        ev.stop();

        var user = document.id('user');
        var passwd = document.id('passwd');
        if(!user.get('value') || !passwd.get('value')) {
            alert('Required Name and Password.');
            return;
        }

        this.getItem = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
            },
            onComplete: function(resp) {
                var user;
                try {
                    user = eval(resp);
                }
                catch(err) { alert('Error...\n' + err); return; }

                this.login(user);
            }.bind(this)
        }).send('table=login&ex=getUser&user=' + user.get('value') + '&passwd=' + passwd.get('value') + '&nc=' + Date.now());

    },
    login: function(user) {

        window.sessionStorage.setItem('user', user.name);
        window.sessionStorage.setItem('uid', user.uid);

        Cookie.write('user', user.name, {duration: 30});
        Cookie.write('mask', user.mask, {duration: 30});
        Cookie.write('seltab', 0, {duration: 30});

        this.loginPanel.slide('out');

        this.fireEvent('login', user);
    },
    hidePanel: function(ev) {
        ev.stop();

        this.loginPanel.slide('out');
    }
});
